﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mjollnir.IO;
using System.IO;
using System.Linq;

namespace Test.Mjollnir
{
    [TestClass]
    public class ByteArrayExtensionsTest
    {
        [TestMethod]
        public void AsStreamTest1()
        {
            var bytes = Enumerable.Range(0, 100 * 1024).Select(_ => (byte)_).ToArray();

            using (var input = bytes.AsStream())
            using (var output = new MemoryStream())
            {
                var buffer = new byte[4096];

                for (var bytesRead = input.Read(buffer, 0, buffer.Length); bytesRead > 0; bytesRead = input.Read(buffer, 0, buffer.Length))
                {
                    output.Write(buffer, 0, bytesRead);
                }

                output.Flush();

                var expected = bytes;
                var actual = output.ToArray();

                Assert.AreEqual(expected.Length, actual.Length);
                Assert.IsTrue(Enumerable.Range(0, expected.Length).All(_ => expected[_] == actual[_]));
            }
        }
    }
}
